package com.zhiyou.app.common.constant;

import cn.dev33.satoken.stp.StpUtil;
import com.zhiyou.app.pojo.bo.LoginUser;
import com.zhiyou.app.pojo.enums.DeviceTypeEnum;
import com.zhiyou.app.pojo.enums.UserTypeEnum;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class UserHandler {
    public static LoginUser getLoginUser() {
        String loginId = StpUtil.getLoginId().toString();
        return parse(loginId);
    }

    public static void userLogin(DeviceTypeEnum deviceTypeEnum, UserTypeEnum userType, Integer userId) {
        StpUtil.login(userType.code + userId, deviceTypeEnum.name());
        StpUtil.getTokenInfo();
    }

    private static LoginUser parse(String loginId) {
        LoginUser loginUser = new LoginUser();
        if (loginId.startsWith(CommonConstant.MINI_USER_PREFIX)) {
            loginUser.setType(UserTypeEnum.mini);
            loginUser.setUserId(Integer.parseInt(loginId.substring(CommonConstant.MINI_USER_PREFIX.length() + 1)));
            return loginUser;
        } else if (loginId.startsWith(CommonConstant.STAFF_PREFIX)) {
            loginUser.setType(UserTypeEnum.staff);
            loginUser.setUserId(Integer.parseInt(loginId.substring(CommonConstant.STAFF_PREFIX.length() + 1)));
            return loginUser;
        }
        return null;
    }
}
